package com.dhcc.bpm.business.papc.controller;

import com.dhcc.bpm.common.annotation.Log;
import com.dhcc.bpm.common.enums.BusinessType;
import com.dhcc.bpm.common.utils.ExcelUtil;
import com.dhcc.bpm.common.utils.http.HttpsUtils;
import io.swagger.annotations.Api;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dhcc.bpm.business.papc.config.BusinessConfig;
import com.dhcc.bpm.business.papc.entity.PrdtInfo;
import com.dhcc.bpm.business.papc.vo.PrdtInfoVO;
import com.dhcc.bpm.business.papc.service.IPrdtInfoService;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.log.annotation.ApiLog;
import java.util.List;

/**
 * 产品基础信息  控制器
 *
 * @author liuzhao
 * @since 2020-05-16
 */
@RestController
@AllArgsConstructor
@RequestMapping("bpm-papc/prdtinfo")
@Api(value = "产品基础信息 ", tags = "产品基础信息 接口")
public class PrdtInfoController extends BladeController {
    private final static Logger log = LoggerFactory.getLogger(PrdtInfoController.class);
    @Autowired
    private BusinessConfig busConfig;
    private IPrdtInfoService prdtInfoService;

    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "详情", notes = "传入prdtInfo")
    public R<PrdtInfo> detail(PrdtInfo prdtInfo) {
//        PrdtInfo detail = prdtInfoService.getOne(Condition.getQueryWrapper(prdtInfo));
        PrdtInfo detail = prdtInfoService.selectPrdtInfoById(prdtInfo.getId());
        return R.data(detail);
    }

    /**
     * 分页 产品基础信息
     */
    @GetMapping("/list")
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "分页", notes = "传入prdtInfo")
    public R<IPage<PrdtInfo>> list(PrdtInfo prdtInfo, Query query) {
        IPage<PrdtInfo> pages = prdtInfoService.page(Condition.getPage(query), Condition.getQueryWrapper(prdtInfo));
        return R.data(pages);
    }

    /**
     * 自定义分页 产品基础信息
     */
    @GetMapping("/page")
    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "分页", notes = "传入prdtInfo")
    public R<IPage<PrdtInfoVO>> page(PrdtInfoVO prdtInfo, Query query) {
        IPage<PrdtInfoVO> pages = prdtInfoService.selectPrdtInfoPage(Condition.getPage(query), prdtInfo);
        return R.data(pages);
    }
    /**
     * 刷新redis
     * @return
     */
 	@ApiLog("刷新redis")
 	@PostMapping("/refush")
 	@ApiOperationSupport(order = 8)
 	@ApiOperation(value = "刷新redis", notes = "传入")
 	@Log(title = "刷新redis", businessType = BusinessType.REFRESH)
 	public R refush() {
 		try {
 			String tableName="ZjywPrdtInfo";
 			String url=busConfig.getBusConfig().getBusurl();
             String response = HttpsUtils.sendSSLPostBodyRequest(url,tableName);
             JSONObject jsons = JSONObject.parseObject(response);
             int retcode=(int) jsons.get("status");
             if(200==retcode) {
 				log.info("刷新配置成功!");
 				return R.status(true);
 			}else {
 				log.error("刷新配置失败");
 				return R.status(false);
 			}
 		}catch (Exception e){
 			log.error("刷新失败:{}", e);
 			return R.status(false);
 		}
 	}
    /**
     * 新增 产品基础信息
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "新增", notes = "传入prdtInfo")
    public R save(@Valid @RequestBody PrdtInfo prdtInfo) {
        return R.status(prdtInfoService.save(prdtInfo));
    }

    /**
     * 修改 产品基础信息
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "修改", notes = "传入prdtInfo")
    public R update(@Valid @RequestBody PrdtInfo prdtInfo) {
        return R.status(prdtInfoService.updateById(prdtInfo));
    }

    /**
     * 新增或修改 产品基础信息
     */
    @PostMapping("/submit")
    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "新增或修改", notes = "传入prdtInfo")
    public R submit(@Valid @RequestBody PrdtInfo prdtInfo) {
        return R.status(prdtInfoService.saveOrUpdate(prdtInfo));
    }


    /**
     * 删除 产品基础信息
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "逻辑删除", notes = "传入ids")
    public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
//        return R.status(prdtInfoService.deleteLogic(Func.toLongList(ids)));
        return R.status(prdtInfoService.deletePrdtInfoByIds(ids));
    }

    @PostMapping("/listPrdtInfoToExcel")
    public void listPrdtInfoToExcel(PrdtInfo prdtInfo, HttpServletResponse response) {
        List<PrdtInfo> prdtInfoList = prdtInfoService.selectPrdtInfoAll(prdtInfo);
        ExcelUtil<PrdtInfo> util = new ExcelUtil<PrdtInfo>(PrdtInfo.class);
        util.exportExcel(prdtInfoList, "产品基础信息表", response);
    }
}
